import axios from 'axios'
import { buildParams, buildPrintParams, drawLabel, ParamsBuilder } from '@/util/label-print'

const printService = '/postek/print'

/**
 * 纯打印
 * @param usbPort
 * @param info {{unitName, name, model, code, department, address, user, logo, value, time}}
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export const print = (usbPort = 255, info) => {
  const builder = new ParamsBuilder()
  builder
    .openUSBPort(usbPort)
    .clearBuffer()
    .setDirection()
    .setPrintSpeed()
    .setDarkness()
    .setLabelHeight()
    .setLabelWidth()
  // 构建标签内容
  drawLabel(builder, info)
  builder.printLabel().closeUSBPort()

  return axios.post(printService, builder.build())
}

export const getPrinters = () => axios.get(`${printService}?reqParam=printerList`)

/**
 * 读取高频标签 UID
 * @param usbPort
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export const getHFUID = (usbPort = 255) => {
  const builder = new ParamsBuilder()
  builder.openUSBPort(usbPort).readHFLabeUID().closeUSBPort()

  return axios.post(printService, builder.build())
}

/**
 * 读取高频 RFID
 * @param usbPort
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export const getHFRfid = (usbPort = 255) => {
  const builder = new ParamsBuilder()
  builder.openUSBPort(usbPort).readHFLabelData().closeUSBPort()

  return axios.post(printService, builder.build())
}

/**
 * 读取超高频 RFID
 * @param usbPort
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export const getRfid = (usbPort = 255) => {
  const builder = new ParamsBuilder()
  builder.openUSBPort(usbPort).readRFIDLabelData().closeUSBPort()

  return axios.post(printService, builder.build())
}

/**
 * 打印超高频 RFID
 * @param usbPort
 * @param info {{unitName, name, model, code, department, address, user, logo, value, time}}
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export const printRfid = (info, usbPort = 255) => {
  return axios.post(printService, buildParams(info, usbPort))
}

export const openUSB = (usbPort = 255) => {
  const builder = new ParamsBuilder()
  builder.openUSBPort(usbPort)
  return axios.post(printService, builder.build())
}

export const closeUSB = () => {
  const builder = new ParamsBuilder()
  builder.closeUSBPort()
  return axios.post(printService, builder.build())
}

export const doPrint = data => {
  return axios.post(printService, buildPrintParams(data))
}

/**
 * 打印高频 RFID
 * @param usbPort
 * @param info {{unitName, name, model, code, department, address, user, logo, value, time}}
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export const printHFRfid = (usbPort = 255, info) => {
  const builder = new ParamsBuilder()
  builder
    .openUSBPort(usbPort)
    .clearBuffer()
    .setDirection()
    .setPrintSpeed()
    .setDarkness()
    .setLabelHeight()
    .setLabelWidth()
    .setHFRFID()
    .rwHFLabel('W', 5, 1, info.code, 0)
  // 构建标签内容
  drawLabel(builder, info)
  builder.printLabel().closeUSBPort()

  return axios.post(printService, builder.build())
}

export const adjustLabel = (usbPort = 255) => {
  const builder = new ParamsBuilder()
  builder
    .openUSBPort(usbPort)
    // .setLabelHeight()
    // .setLabelWidth()
    // .setLabelHeight(20 , 3 )
    // .setLabelWidth(33 )
    .setLabelHeight(50, 3)
    .setLabelWidth(50)
    .closeUSBPort()
  return axios.post(printService, builder.build())
}
